package com.huizu.project.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huizu.common.annotation.Log;
import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.enums.BusinessType;
import com.huizu.project.domain.ProLdgy;
import com.huizu.project.service.IProLdgyService;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.common.core.page.TableDataInfo;

/**
 * 社会公益劳动Controller
 *
 * @author zhangmx
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/project/proldgy")
public class ProLdgyController extends BaseController
{
    @Autowired
    private IProLdgyService proLdgyService;


    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询社会公益劳动列表
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProLdgy proLdgy)
    {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())){
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proLdgy.setBjIds(bjs);
        }
        startPage();
        List<ProLdgy> list = proLdgyService.selectProLdgyList(proLdgy);
        return getDataTable(list);
    }

    /**
     * 导出社会公益劳动列表
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:export')")
    @Log(title = "社会公益劳动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProLdgy proLdgy)
    {
        List<ProLdgy> list = proLdgyService.selectProLdgyList(proLdgy);
        ExcelUtil<ProLdgy> util = new ExcelUtil<ProLdgy>(ProLdgy.class);
        util.exportExcel(response, list, "社会公益劳动数据");
    }

    /**
     * 获取社会公益劳动详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(proLdgyService.selectProLdgyById(id));
    }

    /**
     * 新增社会公益劳动
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:add')")
    @Log(title = "社会公益劳动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProLdgy proLdgy)
    {
        return toAjax(proLdgyService.insertProLdgy(proLdgy));
    }

    /**
     * 修改社会公益劳动
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:edit')")
    @Log(title = "社会公益劳动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProLdgy proLdgy)
    {
        return toAjax(proLdgyService.updateProLdgy(proLdgy));
    }

    /**
     * 删除社会公益劳动
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:remove')")
    @Log(title = "社会公益劳动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(proLdgyService.deleteProLdgyByIds(ids));
    }

    /**
     * 通过
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody List<ProLdgy> proLds)
    {
        for (ProLdgy proLd : proLds) {
            proLdgyService.pass(proLd);
        }
        return toAjax(1);
    }

    /**
     * 驳回
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody List<ProLdgy> proLds)
    {
        for (ProLdgy proLd : proLds) {
            proLdgyService.reject(proLd);
        }
        return toAjax(1);
    }

    /**
     * 打回
     * @return
     */
    @PreAuthorize("@ss.hasPermi('project:proldgy:repulse')")
    @Log(title = "社会公益劳动", businessType = BusinessType.UPDATE)
    @PutMapping("/repulse")
    public AjaxResult repulse(@RequestBody ProLdgy proLd) {
        return toAjax(proLdgyService.repulse(proLd));
    }
}
